package com.hemerasolutions.v2trainer.controller;

import java.io.IOException;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ComponentSystemEvent;
import javax.servlet.ServletException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.hemerasolutions.v2trainer.controller.interfaces.LoginBeanInterface;
import com.hemerasolutions.v2trainer.model.users.User;
import com.hemerasolutions.v2trainer.service.interfaces.UserServiceInterface;

@Controller
@ManagedBean(name="loginBean")
@ViewScoped
public class LoginBean extends BaseBean implements LoginBeanInterface{

	private static final long serialVersionUID = 1L;

	private User user;
	
	private String response;
	
	private String forgottenUser;
	
	@Autowired
	private UserServiceInterface userService;
	

	public void initView(ComponentSystemEvent ev){
	    FacesContext facesContext = FacesContext.getCurrentInstance();
	      
	    if (!facesContext.isPostback() && !facesContext.isValidationFailed()) {
			if (this.response != null && !this.response.equals("") ) { 
		    	this.showErrorMessage(	this.localizedText("Error"),
    			this.getContextValue("#{SPRING_SECURITY_LAST_EXCEPTION.message}", String.class));
		    	this.updatePFComponnent(":growlLogin");
		    }
	    	
	    }	
	}
	
	
	// Chamado ao logar
	@Override
	public String login() throws IOException, ServletException{

        this.response = null;
		
		ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();

        RequestDispatcher dispatcher = ((ServletRequest) context.getRequest()).getRequestDispatcher("/j_spring_security_check");
        
        dispatcher.forward((ServletRequest) context.getRequest(),(ServletResponse) context.getResponse());
 
        FacesContext.getCurrentInstance().responseComplete();

        return null;
    }

	@Override
	public String goForgetPassword() throws IOException,ServletException{

		return "forgotpassword.jsf";

	}

	@Override
	public void sendPassword(ActionEvent ev){
		
		if(userService.sendPassword(userService.getUserByUserEmail(this.forgottenUser))){
			this.showInfoMessage(this.localizedText("Success"),this.localizedText("EmailSent"));
	    } else {
	    	this.showErrorMessage(	this.localizedText("ErrorSendingEmail"), 
	    							this.localizedText("ContactTheAdministrator"));
	    }
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getForgottenUser() {
		return forgottenUser;
	}

	public void setForgottenUser(String forgottenUser) {
		this.forgottenUser = forgottenUser;
	}

	public String getResponse() {
		return response;
	}

	public void setResponse(String response) {
		this.response = response;
	}


}


